<?php

class SuggesteditController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {
    	$this->view->setMainView('index');
		$this->view->setVar('page_name', 'SuggestEdit');
		$this->view->setVar('content', 'suggestedit/index');
		$this->view->setVar('title', 'Suggest Editor');
    }

    public function saveAction()
    {
        $str = $this->request->getPost('data');
        $data = array();
        $for_saving = array();
        $result = array();
        parse_str($str,$data);

        if(!empty($data['company_id']))
        {
            $for_saving['company_id'] = $data['company_id'];         
        }
        if(!empty($data['name']))
        {
            $for_saving['name'] = $data['name'];         
        }
        if(!empty($data['location']))
        {
            $for_saving['location'] = $data['location'];         
        }
        if(!empty($data['city']))
        {
            $for_saving['city'] = $data['city'];         
        }
        if(!empty($data['about']))
        {
            $for_saving['about'] = $data['about'];         
        }
        if(!empty($data['contact_no']))
        {
            $for_saving['contact_no'] = $data['contact_no'];         
        }
        if(!empty($data['email']))
        {
            $for_saving['email'] = $data['email'];         
        }
        if(!empty($data['noofworkers']))
        {
            $for_saving['noofworkers'] = $data['noofworkers'];         
        }
        $for_saving['suggestor_id'] = $this->session->get('user_id');

        $this->db->begin();
        $suggest = new ClientInfoSuggests();
        if(!$suggest->save($for_saving)){
            $messages = '';
            foreach ($suggest->getMessages() as $value) {
                $messages.=$value."\n";
            }
            $result['status'] = false;
            $result['messages']= $messages;
            echo json_encode($result);
            $this->view->disable();
            exit();
        }
        $this->db->commit();
        $result['status'] = true;
        echo json_encode($result);
        $this->view->disable();
    }
    public function approveAction()
    {
    	$cis = (int) $this->request->getPost('cis');
    	$company_id = $this->request->getPost('cid');
    	$result = array();
        $data = array();

    	$this->db->begin();
    	$messages = '';

        $cis_info = ClientInfoSuggests::findFirstById($cis);
    
        if(!empty($cis_info->name))
        {
            $data["name"] = $cis_info->name;
        }
        if(!empty($cis_info->about))
        {                     
            $data["about"] = $cis_info->about;
        }
        if(!empty($cis_info->city))
        {
            $data["city"] = $cis_info->city;
        }
        if(!empty($cis_info->location))
        {
            $data["location"] = $cis_info->location;
        }
        if(!empty($cis_info->contact_no))
        {
            $data["contact_no"] = $cis_info->contact_no;
        }
        if(!empty($cis_info->email))
        {
            $data["email"] = $cis_info->email;
        }
        if(!empty($cis_info->noofworkers))
        {
            $data["noofworkers"] = $cis_info->noofworkers;
        }    

    	$company = ClientInfo::findFirstById($company_id);
    	if(!$company->save($data)){
    		foreach ($company->getMessages() as $value) {
    			$messages .= $value . "<br>";
    		}
    		$result['status'] = false;
    		$result['messages']= $messages;
    		echo json_encode($result);
    		$this->view->disable();
    		exit();
    	}

    	$suggest = ClientInfoSuggests::findFirstById($cis);
    	$suggest->status = 1;
    	$suggest->save();
    	if(!$suggest->save()){
    		foreach ($suggest->getMessages() as $value) {
    			$messages .= $value . "<br>";
    		}
    		$result['status'] = false;
    		$result['messages']= $messages;
    		echo json_encode($result);
    		$this->view->disable();
    		exit();
    	}
    	$this->db->commit();
    	$result['status'] = true;
        $result['data'] = $data;
    	echo json_encode($result);
    	$this->view->disable();
    }
}

